print("hello")
# 基础数据类型：数字、字符串、列表、元组、字典和集合
# 数字-int
a=3
# 数字-float
b=1.2
# 数字-complex
b=1+4j
# 字符串-str
b="adf"
b='asd'
b="""afds"""#多行字符串
i=0
print(f'f格式化字符串第{i+1}个数是{b}')#string-f 格式化字符串 F或者f都可以
text = u'unicode字符串'
text=b'12334asd123'
table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
print(f'Jack: {table["Jack"]}; Sjoerd: {table["Sjoerd"]}; Dcab: {table["Dcab"]}')
print('Jack: {0[Jack]:d}; Sjoerd: {0[Sjoerd]:d}; Dcab: {0[Dcab]:d}'.format(table))
print(text)
# 列表-list
b=[1,1.2,"python"]
# 元组-tuple  有序集合，内容不可见
b=(1,1.2,"python")
# 字典-dict   无序的键值对集合
b={"name":"陈志源"}
# 集合-set    无序且不重复
b={"name","name","陈志源"}
# 布尔值 bool 1==True 0==False 布尔值前加not表示取反
b=True
b=False

#控制结构
#条件 pass语法上需要一个语句，但是实际上不需要执行时
b=0
if(b):
    print("不会进来的语句")
if(b):
    pass
else:
    print("else进")
if b:
    print("")
elif (b):
    print("asasdfdf")
else:
    print("")
# 循环 break跳出循环 continue跳过当次循环，继续下一次
for item in ["1","2"]:
    print(item)
i=0
while i<5:
    print("while循环")
    i+=1

#列表式推导
# new_list = [expression for item in iterable if condition]
# expression：对每个元素进行的操作 item：迭代变量。
# iterable：可迭代对象，如列表、元组、字符串等 if condition：可选的条件，只有满足条件的元素才会被包含在新列表中。
squares = [x**2 for x in range(10) if x % 2 == 0]
print(squares)

#异常处理
try:
    x = 10 / 1
except ZeroDivisionError:
    print("除零错误")
else:
    print("没有异常")
finally:
    print("无论如何都会执行")

#函数的定义
def add(a, b):
    """Adds two numbers and returns the result."""
    return a + b
# 调用函数
# aa=add(1,2)
# print(aa)

#读取文件
def read(filename):
    with open(filename, 'r', encoding='utf-8') as file:
        content = file.read()
        return content
text=read("README.md")

#逐行读取
def read_line_by_line(filename):
    with open(filename,'r',encoding='utf-8') as file:
        for line in file:
            print(line.strip()) # 使用 strip() 去除行末的换行符
def write_line_by_line(filename,arrays):
    with open(filename, 'w',encoding='utf-8') as file:
        file.writelines(arrays)
        # for line in arrays:
        #     file.writelines(line)
        #     file.write("This is another line.\n")

# 追加内容到文件
def append(filename,text):
    with open(filename, 'a',encoding='utf-8') as file:
        file.write(text+'\n')